﻿using System;
using System.Data;
using System.Drawing;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using HiauPdm.Bll;
using HiauPdm.Web;

namespace HiauPdm.Web
{
    public partial class WgjWdBj : PageBase
    {
        protected override bool IsAccess()
        {
            return IsAccess("WgjBj.aspx");
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                TextId.Value = Request.QueryString["WgjId"];
                BtnSave.OnClientClick = "this.disabled=true;" + Page.ClientScript.GetPostBackEventReference(BtnSave, null) + ";return false;";
                BindData();
                TextMc.Focus();
            }
        }

        private void BindData()
        {
            if ((TextId.Value.Trim() != string.Empty) && BGlobal.IsNumeric(TextId.Value.Trim()))
            {
                DataSet ds = BWgjBj.GetWgjBjById(Convert.ToInt32(TextId.Value.Trim()));
                if ((ds != null) && (ds.Tables[0].Rows.Count == 1))
                {
                    PanelEdit.Visible = true;
                    BtnSave.Enabled = true;
                    TextDh.Text = ds.Tables[0].Rows[0]["代号"].ToString();
                    TextLb.Text = ds.Tables[0].Rows[0]["类别"].ToString();
                    TextMc.Text = ds.Tables[0].Rows[0]["名称"].ToString();
                    TextGgxh.Text = ds.Tables[0].Rows[0]["规格型号"].ToString();
                    TextDw.Text = ds.Tables[0].Rows[0]["单位"].ToString();
                    TextPpcj.Text = ds.Tables[0].Rows[0]["品牌厂家"].ToString();
                    TextBz.Text = ds.Tables[0].Rows[0]["备注"].ToString();
                    TextBjr.Text = ds.Tables[0].Rows[0]["编辑人"].ToString();
                    TextBjsj.Text = ds.Tables[0].Rows[0]["编辑时间"].ToString();
                    if (TextBjr.Text != (string)Session["RealName"] && !IsAccess("编辑所有外购件"))
                    {
                        TextMc.ReadOnly = true;
                        TextGgxh.ReadOnly = true;
                        TextDw.ReadOnly = true;
                        TextPpcj.ReadOnly = true;
                        TextBz.ReadOnly = true;
                        TextMc.BackColor = Color.FromArgb(0xE7F3FF);
                        TextGgxh.BackColor = Color.FromArgb(0xE7F3FF);
                        TextDw.BackColor = Color.FromArgb(0xE7F3FF);
                        TextPpcj.BackColor = Color.FromArgb(0xE7F3FF);
                        TextBz.BackColor = Color.FromArgb(0xE7F3FF);
                    }
                    BindWgjWdData(TextDh.Text.Trim());
                }
            }
        }

        private void BindWgjWdData(string sWgjDh)
        {
            DataSet ds = BWgjBj.GetWgjWdByWgjDh(sWgjDh);
            if (ds != null)
            {
                ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            }
            GridWgjWd.DataSource = ds;
            GridWgjWd.DataBind();
            SetCzQx();
        }

        private void SetCzQx()
        {
            if (TextBjr.Text != (string)Session["RealName"] && !IsAccess("编辑所有外购件"))
            {
                for (int i = 0; i < (GridWgjWd.Rows.Count - 1); i++)
                {
                    if ((string)GridWgjWd.DataKeys[i].Values[2] != (string)Session["RealName"])
                    {
                        ((TextBox)GridWgjWd.Rows[i].Cells[1].Controls[1]).ReadOnly = true;
                        ((TextBox)GridWgjWd.Rows[i].Cells[2].Controls[1]).ReadOnly = true;
                        ((CheckBox)GridWgjWd.Rows[i].Cells[3].Controls[1]).Enabled = false;
                        ((FileUpload)GridWgjWd.Rows[i].Cells[1].Controls[3]).Visible = false;
                        GridWgjWd.Rows[i].Cells[6].Controls[1].Visible = false;
                        GridWgjWd.Rows[i].Cells[6].Controls[3].Visible = false;
                    }
                }
            }
        }

        protected void BtnSave_Click(object sender, EventArgs e)
        {
            if (ShowErr(BWgjBj.SaveWgjWd(Convert.ToInt32(TextId.Value.Trim()), TextDh.Text, TextMc.Text, TextGgxh.Text, TextDw.Text, TextPpcj.Text, TextBz.Text, (string)Session["RealName"], GridWgjWd)))
            {
                BindWgjWdData(TextDh.Text.Trim());
            }
            else
            {
                Response.Redirect(Request.Url.PathAndQuery);
            }
        }

        protected void GridWgjWd_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if ((e.Row.RowType == DataControlRowType.DataRow) && (GridWgjWd.DataKeys[e.Row.DataItemIndex].Value.ToString() == string.Empty))
            {
                ((Literal)e.Row.Cells[0].Controls[1]).Text = "添加";
                e.Row.Cells[6].Controls[1].Visible = false;
                e.Row.Cells[6].Controls[3].Visible = false;
            }
        }

        protected void GridWgjWd_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "DownloadDoc")
            {
                Response.Redirect("DownloadFile.aspx?id=" + BGlobal.Encrypt(string.Format("{0}|{1}.{2}", GridWgjWd.DataKeys[Convert.ToInt32(e.CommandArgument)].Values[1], ((TextBox)GridWgjWd.Rows[Convert.ToInt32(e.CommandArgument)].Cells[1].Controls[1]).Text.Trim(), BGlobal.HexToString(GridWgjWd.DataKeys[Convert.ToInt32(e.CommandArgument)].Values[1].ToString().Split('_')[1])), "Download"));
            }
        }

        protected void GridWgjWd_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            if (!ShowErr(BWgjBj.DelWgjWd((int)GridWgjWd.DataKeys[e.RowIndex].Value)))
            {
                Response.Redirect(Request.Url.PathAndQuery);
            }
        }
    }
}